﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AgileTimeTracker.Client.Wpf.Common;
using AgileTimeTracker.Client.Wpf.Services;
using AgileTimeTracker.Domain.Exceptions;

namespace AgileTimeTracker.Client.Wpf.AccessControlModule
{
    public class SignUpViewModel : PageViewModel
    {
        private string login;        

        public SignUpViewModel()
        {
            this.Title = "Sign Up";
        }

        public string Login
        {
            get { return this.login; }
            set { this.OnPropertyChanged(ref this.login, value); }
        }

        public Func<string> GetPassword;

        public RelayCommand SignUpCommand { get; set; }

        protected override void InititalizeCommands()
        {
            base.InititalizeCommands();
            this.SignUpCommand = new RelayCommand(this.SignUp);
        }

        protected override void Close()
        {
            base.Close();
            MainWindowViewModel.Instance.CurrentView = new LoginViewModel();
        }

        private void SignUp()
        {
            var password = this.GetPassword();
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new DomainException("The field password is required.");
            }

            if (string.IsNullOrWhiteSpace(this.login))
            {
                throw new DomainException("The field login is required.");
            }

            var user = AppService.Instance.CreateUser(this.login, this.GetPassword());            
            MainWindowViewModel.Instance.CurrentUser = user;
            MainWindowViewModel.Instance.CurrentView = new ProjectSelectionViewModel();
        }
    }
}
